<template>
	<el-container>
		<!-- <el-aside width="200px">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="id" :data="groupData" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
				</el-main>
			</el-container>
		</el-aside> -->
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="list.apiObj" :params="{device_id: this.$route.params.device_id}" row-key="id" @selection-change="selectionChange" stripe>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="Id" prop="id" width="180"></el-table-column>
					<el-table-column label="设备名字" prop="device.name" width="180"></el-table-column>
					<el-table-column label="线序名字" prop="name" width="180"></el-table-column>
					<el-table-column label="设备线序" prop="sort_num" width="180"></el-table-column>
					<el-table-column label="状态" prop="status" width="60">
						<template #default="scope">
							<sc-status-indicator v-if="scope.row.status" type="success"></sc-status-indicator>
							<sc-status-indicator v-if="!scope.row.status" pulse type="danger"></sc-status-indicator>
						</template>
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="center" width="160">
						<template #default="scope">
							<router-link v-if="scope.row.type == 2" :to="{name: 'branch', params: { device_id: scope.row.id }}">
									<el-button text type="primary" size="small">管理</el-button>
							</router-link>
							<el-button-group>	
								<el-button text type="primary" size="small" @click="row_show(scope.row)">查看</el-button>
								<el-button text type="primary" size="small" @click="row_edit(scope.row)">编辑</el-button>
								<el-button text type="primary" size="small" @click="row_del(scope.row, scope.$index)">删除</el-button>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveD" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
	import saveDialog from "./save.vue";
	
	export default {
		components: {
			saveDialog,
		},
		name: 'listTree',
		data() {
			return {
				dialog:{
					save: false,
					//info: false,
				},
				selection: [],
				list: {
					apiObj: this.$API.branch.list
				},
				search: {
					keyword: ""
				}
			}
		},
		mounted() {
			
		},
		watch: {
			// groupFilterText(val) {
			// 	this.$refs.group.filter(val);
			// }
		},
		methods: {	
			add() {
				this.dialog.save=true
				this.$nextTick(() => {
					let data = {'device_id': this.$route.params.device_id}
					this.$refs.saveD.open('add').setData(data)
				})
			},
			row_show(val) { //  查看功能
				this.dialog.save=true
				this.$nextTick(() => {
					this.$refs.saveD.open('show').setData(val);
				})
			},
			row_edit(val) { //  编辑功能
				this.dialog.save=true
				this.$nextTick(() => {
					this.$refs.saveD.open('edit').setData(val);
				})
			},
			async row_del(val, index) { //  删除功能
				console.log(index)
				var reqData = val.id
				var res = await this.$API.branch.delete.delete(reqData);
				if(res.code == 200){
					//this.$refs.table.removeIndex(index)
					this.$message.success("删除成功")
					this.$refs.table.refresh()
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection
			},
			//批量删除
			async batch_del(){
				var confirmRes = await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '删除',
					confirmButtonClass: 'el-button--danger'
				}).catch(() => {})
				if(!confirmRes){
					return false
				}	
				var ids = this.selection.map(v => v.id)
				this.$refs.table.removeKeys(ids)
				var res = await this.$API.branch.delete.delete(ids);
				if(res.code == 200){
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				console.log(data,mode)
				//为了减少网络请求，直接变更表格内存数据
				// if(mode=='add'){
				// 	this.$refs.table.unshiftRow(data)
				// }else if(mode=='edit'){
				// 	console.log(data)
				// 	this.$refs.table.updateKey(data)
				// }
				//当然也可以暴力的直接刷新表格
				this.$refs.table.refresh()
			}
		}
	}
</script>

<style scoped>
.choose {
	margin-left: 20px
}
.reset {
	margin-left: 10px
}
</style>
